553C - Love Triangles - CodeForces Solution


dfs and similar dsu graphs *2200

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
vector <int> a[100001],c[100001];
int d[100001],n,m,u,v,k,kq,f[100001];
const int mod=round(1e9)+7;
void DFS(int u,int p)
{
    d[u]=1;
    for(int i=0;i<a[u].size();i++)
    {
        int v=a[u][i];
        if(d[v]==0)
        {
            f[v]=f[u]^c[u][i];
            DFS(v,u);
        }
        else if((f[v]^f[u]^c[u][i])==1) { cout<<0; exit(0); }
    }
}
int main()
{
    //freopen("ntu.inp","r",stdin);
    //freopen("ntu.out","w",stdout);
    cin>>n>>m;
    while(m--)
    {
        cin>>u>>v>>k; k=1-k;
        a[u].push_back(v); a[v].push_back(u);
        c[u].push_back(k); c[v].push_back(k);
    }
    for(u=1;u<=n;u++)
        if(d[u]==0)
        {
            DFS(u,0);
            if(kq==0) kq=1; else kq=kq*2%mod;
        }
    cout<<kq;
}


Comments

Submit
0 Comments
More Questions

Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses
Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble